﻿using System.ServiceModel;

using SBPweb.PROvibe.Server.Core.Modularity;
using SBPweb.PROvibe.Server.Library.Services;

using SBPweb.PROvibe.Server.Modules.DataProvider.Contracts;
using SBPweb.PROvibe.Server.Modules.ServiceProvider.Contracts;
using SBPweb.PROvibe.Server.Modules.UserProvider;
using SBPweb.PROvibe.Server.Modules.UserProvider.Contracts;

using SBPweb.PROvibe.Shared.Library.Base;
using SBPweb.PROvibe.Shared.Library.Components;

namespace SBPweb.PROvibe.Server.Core.Services
{

    public abstract class CoreServiceBase<T> : ServiceBase<T>
        where T : IService
    {

        #region Constructor logic

        public CoreServiceBase()
            : this(null)
        {
        }

        public CoreServiceBase(IModule parentModule)
            : base(parentModule)
        {
        }

        #endregion

        #region Properties

        public override IModule Parent
        {
            get
            {
                if (base.Parent == null)
                {
                    return ServerContext.Instance.ModuleManager.Modules.FirstByType<CoreModule>();
                }
                return base.Parent;
            }
        }

        protected internal IDataProviderService DataProvider
        {
            get
            {
                return ServerContext.Instance.ServiceManager.Services.FirstByType<IDataProviderService>();
            }
        }

        protected internal IServiceProviderService ServiceProvider
        {
            get
            {
                return ServerContext.Instance.ServiceManager.Services.FirstByType<IServiceProviderService>();
            }
        }

        protected internal IUserProviderService UserProvider
        {
            get
            {
                return ServerContext.Instance.ServiceManager.Services.FirstByType<IUserProviderService>();
            }
        }

        protected internal User ImpersonatedUser
        {
            get
            {
                if (UserProvider == null)
                {
                    return null;
                }
                return UserProvider.GetUser(OperationContext.Current.ServiceSecurityContext.PrimaryIdentity);
            }
        }
        
        #endregion

        #region Methods

        protected override void OnInitialize()
        {
            
        }

        protected override void OnTerminate()
        {
            
        }

        #endregion

    }

}